package top.bug.blog.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import top.bug.blog.common.BasePO;
import top.bug.blog.constant.ArticleConstants;
import top.bug.blog.constant.CommonConstants;
import top.bug.blog.entity.po.Article;
import top.bug.blog.entity.po.Tag;
import top.bug.blog.entity.vo.home.StatistiscVO;
import top.bug.blog.service.ArticleService;
import top.bug.blog.service.HomeService;
import top.bug.blog.service.TagService;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author <a href="https://github.com/jiusi66">九思.</a>
 * @email 10837907@qq.com
 * @from <a href="https://www.52bug.top">递归茶馆-www.52bug.top</a>
 * @description 首页 实现类
 */
@Service
public class HomeServiceImpl implements HomeService {
    @Resource
    private ArticleService articleService;
    @Resource
    private TagService tagService;

    @Override
    public StatistiscVO getHomeStatistisc() {
        StatistiscVO statistiscVO = new StatistiscVO();
        // 获取文章数、访问数、标签数、最后更新时间
        long articlaNum = articleService.count(new LambdaQueryWrapper<Article>()
                .eq(Article::getStatus, ArticleConstants.ARTICLE_STATUS_PUBLISH)
                .ne(Article::getType, ArticleConstants.ARTICLE_TYPE_PRIVATE)
        );
        Article article = articleService.getOne(new LambdaQueryWrapper<Article>()
                .orderByDesc(BasePO::getUpdateOn).last("limit 1")
        );
        // 默认最后更新时间为当前时间
        LocalDateTime lastUpdateTime = LocalDateTime.now();
        if (null != article) {
            lastUpdateTime = article.getUpdateOn();
        }
        statistiscVO.setArticlaNum(articlaNum);
        statistiscVO.setLastUpdateTime(lastUpdateTime);
        long tagNum = tagService.count(new LambdaQueryWrapper<Tag>()
                .eq(Tag::getStatus, CommonConstants.STATUS_ENABLE)
        );
        statistiscVO.setTagNum(tagNum);
        // todo：获取访问数
        // todo: 获取系统运行时间
        statistiscVO.setVisitorNum(RandomUtil.randomLong(10000));
        statistiscVO.setSysRunDays(RandomUtil.randomLong(999));
        return statistiscVO;
    }
}